package com.zhidisoft.system.vo;

import java.util.Set;

import org.apache.commons.lang3.StringUtils;

import com.zhidisoft.system.entity.Role;
import com.zhidisoft.system.entity.User;
import com.zhidisoft.util.DateUtil;

public class UserVO {
	
	private String id;
	
	private String username;
	
	private String password;
	
	private String salt;
	
	private String phone;
	
	private String email;
	
	private String createBy;
	
	private String createById;
	
	private String createTime;
	
	private String updateBy;
	
	private String updateById;
	
	private String updateTime;
	
	private String roleNames;
	
	private String roleIds;
	
	private String userNote;
	
	private Integer sortNum;
	
	private Integer status;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getSalt() {
		return salt;
	}

	public void setSalt(String salt) {
		this.salt = salt;
	}

	public String getCreateBy() {
		return createBy;
	}

	public void setCreateBy(String createBy) {
		this.createBy = createBy;
	}

	
	public String getCreateTime() {
		return createTime;
	}

	public void setCreateTime(String createTime) {
		this.createTime = createTime;
	}

	public String getUpdateBy() {
		return updateBy;
	}

	public void setUpdateBy(String updateBy) {
		this.updateBy = updateBy;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}

	public String getRoleNames() {
		return roleNames;
	}

	public void setRoleNames(String roleNames) {
		this.roleNames = roleNames;
	}
	
	
	
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}
	
	

	public String getUserNote() {
		return userNote;
	}

	public void setUserNote(String userNote) {
		this.userNote = userNote;
	}

	public Integer getSortNum() {
		return sortNum;
	}

	public void setSortNum(Integer sortNum) {
		this.sortNum = sortNum;
	}

	public Integer getStatus() {
		return status;
	}

	public void setStatus(Integer status) {
		this.status = status;
	}
	

	public String getRoleIds() {
		return roleIds;
	}

	public void setRoleIds(String roleIds) {
		this.roleIds = roleIds;
	}
	
	

	public String getCreateById() {
		return createById;
	}

	public void setCreateById(String createById) {
		this.createById = createById;
	}

	public String getUpdateById() {
		return updateById;
	}

	public void setUpdateById(String updateById) {
		this.updateById = updateById;
	}

	public static UserVO fromUser(User user){
		if(user!=null){
			UserVO vo = new UserVO();
			
			vo.setId(user.getId());
			vo.setUsername(user.getUsername());
			vo.setPassword(user.getPassword());
			vo.setSalt(user.getSalt());
			vo.setEmail(user.getEmail());
			vo.setPhone(user.getPhone());
			
			vo.setUserNote(user.getUserNote());
			vo.setStatus(user.getStatus());
			vo.setSortNum(user.getSortNum());
			
			vo.setCreateTime( DateUtil.formatDate(user.getCreateTime(), "yyyy-MM-dd HH:mm:ss") );
			vo.setUpdateTime( DateUtil.formatDate(user.getUpdateTime(), "yyyy-MM-dd HH:mm:ss") );
			
			if(user.getCreateUser()!=null){
				vo.setCreateBy(user.getCreateUser().getUsername());
				vo.setCreateById(user.getCreateUser().getId());
			}
			
			if(user.getUpdateUser()!=null){
				vo.setUpdateBy(user.getUpdateUser().getUsername());
				vo.setUpdateById(user.getUpdateUser().getId());
			}
			
			Set<Role> roles = user.getRoles();
			StringBuffer roleNames = new StringBuffer();
			StringBuffer roleIds = new StringBuffer();
			for(Role role : roles){
				roleIds.append(role.getId());
				roleIds.append(",");
				roleNames.append(role.getRoleName());
				roleNames.append(",");
			}//超级管理员,销售员
			if(roleNames.length()>0){
				roleNames.deleteCharAt(roleNames.length()-1);
				roleIds.deleteCharAt(roleIds.length()-1);
			}
			vo.setRoleNames(roleNames.toString());
			vo.setRoleIds(roleIds.toString());
			
			return vo;
		}
		return null;
	}

	public static User toUser(UserVO userVO) {
		if(userVO!=null){
			User user = new User();
			user.setId(userVO.getId());
			user.setUsername(userVO.getUsername());
			user.setPassword(userVO.getPassword());
			user.setEmail(userVO.getEmail());
			user.setPhone(userVO.getPhone());
			user.setSortNum(userVO.getSortNum());
			user.setSalt(userVO.getSalt());
			user.setUserNote(userVO.getUserNote());
			user.setStatus(userVO.getStatus());
			
			user.setCreateTime( DateUtil.parseDate(userVO.getCreateTime(), "yyyy-MM-dd HH:mm:ss") );
			user.setUpdateTime( DateUtil.parseDate(userVO.getUpdateTime(), "yyyy-MM-dd HH:mm:ss") );
			
			if(StringUtils.isNotBlank(userVO.getCreateById())){
				User createUser = new User();
				createUser.setId(userVO.getCreateById());
				user.setCreateUser(createUser);
			}
			
			if(StringUtils.isNotBlank(userVO.getUpdateById())){
				User updateUser = new User();
				updateUser.setId(userVO.getUpdateById());
				user.setUpdateUser(updateUser);
			}
			
			if(StringUtils.isNotBlank( userVO.getRoleIds() )){
				
				String[] roleIds = userVO.getRoleIds().split(",");
				for(String roleId : roleIds){
					Role role = new Role();
					role.setId(roleId);
					user.getRoles().add(role);
				}
			}
			
			return user;
		}
		return null;
	}

}
